package com.rdc.qingkou.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.contract.UserInfoContract;
import com.rdc.qingkou.entity.ModifyUserInfoResult;
import com.rdc.qingkou.presenter.UserInfoPresenter;
import com.rdc.qingkou.utils.ACache;

import java.io.File;

import static com.rdc.qingkou.utils.MyUtils.getFileFromBytes;

/**
 * Created by WaxBerry on 2017/11/16.
 */

public class ModifySignatureActivity extends AppCompatActivity implements UserInfoContract.IView{


    private ACache aCache;
    private EditText edit_modify;
    private Button btn_choose;
    private Context context;

    private byte[] binary;
    private File image;
    private UserInfoPresenter userInfoPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        setContentView(R.layout.activity_modify_signature);
        context = this;

        aCache = ACache.get(this);

        btn_choose = (Button)findViewById(R.id.btn_choose);

        userInfoPresenter = new UserInfoPresenter(this);

        edit_modify = (EditText)findViewById(R.id.edit_modify);
        if (aCache.getAsString("signature") != null) {
            edit_modify.setText(aCache.getAsString("signature"));
        }else {
            edit_modify.setText("每一首情歌忽然充满意义，我就在此刻突然见到你");
        }

        //禁止回车键换行
        edit_modify.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                return(event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
            }
        });
        edit_modify.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                btn_choose.setVisibility(View.GONE);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                btn_choose.setVisibility(View.VISIBLE);
            }

            @Override
            public void afterTextChanged(Editable s) {
                btn_choose.setVisibility(View.VISIBLE);
            }
        });

        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binary = aCache.getAsBinary("sculpture");

                image = getFileFromBytes(binary,"file:///sdcard/output_image2.jpg");

                userInfoPresenter.modifyUserInfo(image,
                        Integer.parseInt(aCache.getAsString("gender")),
                        aCache.getAsString("nickName"),
                        edit_modify.getText().toString(),
                        MyApplication.getUser().getUserId());
            }
        });
    }

    @Override
    public void showData(ModifyUserInfoResult modifyUserInfoResult) {

    }

    @Override
    public void showModifyResult(ModifyUserInfoResult modifyUserInfoResult) {

        Log.d("ModifyNameActivity","SHOW MODIFY RESULT : iamgeUrl is" + modifyUserInfoResult.getUser().getImageUrl() +
                "Signature is :"+ modifyUserInfoResult.getUser().getSignature());
        Toast.makeText(this,"更改签名成功！",Toast.LENGTH_SHORT).show();
        finish();

    }

    @Override
    public void showInfo(String info) {
        Log.d("SculpturePreActivity","ERROR :" + info);
    }
}